package io;

import static net.mindview.util.Print.print;
import static net.mindview.util.Print.printnb;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class LargeMappedFiles {
//    static int length = 0x8FFFFFF; // 128 MB，0x8000000除以1m可以得到128。这里不太清楚为什么写作128
    static int length = 0x2000000;

    public static void main(String[] args) throws Exception {
        MappedByteBuffer out = new RandomAccessFile("src/main/java/io/test.dat", "rw").getChannel()
                .map(FileChannel.MapMode.READ_WRITE, 0, length);
        for (int i = 0; i < length; i++)
            out.put((byte)'x');
        print("Finished writing");
        for (int i = length / 2; i < length / 2 + 6; i++)
            printnb((char) out.get(i));
    }
}
